﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using SAServices.Model;

namespace SAServices.RemtInvtor
{
    public class InvtRmtClient
    {
        private InvtRmtServiceInterface remoteObject = null;

        private TcpChannel tcpChannel = null;

        public InvtRmtClient(string strIpAddress,string strPort)
        {
            tcpChannel = new TcpChannel();

            ChannelServices.RegisterChannel(tcpChannel, false);

            

            Type requiredType = typeof(InvtRmtServiceInterface);

            remoteObject = (InvtRmtServiceInterface)Activator.GetObject(requiredType,
                "tcp://" + strIpAddress + ":" + strPort + "/" + "InventorStart");

        }

        public string SayHello(string who)
        {
            try
            {

                return remoteObject.SayHello(who);
            }

            catch (Exception ex)
            {
                return "SayHello:" + ex.Message;
            }
        }
        public void DisConnection()
        {
            if (tcpChannel != null)
            {
                ChannelServices.UnregisterChannel(tcpChannel);
            }
        }

        public bool IsInventorReady()
        {
            try
            {

                return remoteObject.IsInventorReady();
            }
            
            catch (Exception ex)
            {
                return false; 
            }
        }

        public List<ParameterInfo> GetParameters(string modelPath)
        {
            try
            {

                return remoteObject.GetParameters(modelPath);
            }

            catch (Exception ex)
            {
                return null;
            }
        }

        public bool  GeneratePart(string templatePath, List<ParameterInfo> driveParams, string targetPath)
        {
            try
            {

                return remoteObject.GeneratePart(templatePath, driveParams, targetPath);
            }

            catch (Exception ex)
            {
                return false;
            }
        }

        public bool DeleteCache(Guid familyId)
        {
            try
            {

                return remoteObject.DeleteCache(familyId);
            }

            catch (Exception ex)
            {
                return false;
            }
        }

        public bool GenerateCache(string templatePath, List<ParameterInfo> driveParams, List<string> cachePaths)
        {
            try
            {

                return remoteObject.GenerateCache(templatePath, driveParams, cachePaths);
            }

            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
